<?php
	namespace Admin\Controller;
	use Think\Controller;
	/**
	 *
	积分控制器 2016-11-09 @auth:姚飞
	*/

	class LevelController extends Controller
	{
		// 积分页
		public function level()
		{
			$data = M('level')->select();
			
			$socreArr = array();
			foreach ($data as $key => $value) {
				
				$socreArr[] = json_decode($value['socre']);
				$socre[]	= $socreArr[$key]->min.'-'.$socreArr[$key]->max;

				$val []		= array_merge($data[$key], array('soc' => $socre[$key]));
				
			}
			$this->assign('title','积分规则');
			$this->assign('list',$val);
			$this->display();
		}

		// 添加积分页
		public function add()
		{
			$this->assign('title','积分添加');
			$this->display();
		}

		// 执行添加操作
		public function insert()
		{
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728 ;// 设置附件上传大小
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
			// 上传单个文件
			$info = $upload->uploadOne($_FILES['img']);

	        // 过滤数据
	        $level = D('level');

	        if($_POST['socre1'] > $_POST['socre2']){

	        	$max = $_POST['socre1'];
	        	$min = $_POST['socre2'];

	        }else if($_POST['socre1'] < $_POST['socre2']){
	        	$max = $_POST['socre2'];
	        	$min = $_POST['socre1'];
	        }

	        $range = ['min'=>$min,'max'=>$max];

	        $range = json_encode($range);

			// 保存当前数据对象
			$_POST['img'] = $info['savepath'].$info['savename'];
	        
	        $data = array('level'=>$_POST['level'],'img'=>$_POST['img'],'socre'=>$range);

	        if (empty($_POST['img']) || empty($_POST['level']) || empty($_POST['socre1']) || empty($_POST['socre2'])) {
	            $this->error('请填写数据!',U('Admin/Level/add'));
	            exit;
	        }

            if ($level->add($data) > 0) {
                $this->success('恭喜您,添加成功!',U('level'));
            } else {
                $this->error('添加失败!');
            }
		}

		// 执行删除操作
	    public function del()
	    {
	        // 判断有无传递参数
	        if(empty($_GET['id'])){
	            $this->redirect('Admin/Level/level');
	            exit;
	        }

	        // 接收参数
	        $id = I('get.id/d');

	        //删除操作
	        if (M('level')->delete($id) > 0) {
	            $this->success('恭喜您,删除成功!',U('level'));
	        } else {
	            $this->error('删除失败!!!!!');
	        }
	    }

	    // 修改积分页
	    public function edit()
	    {
	    	// 接收参数
	        $id = I('get.id/d');

	        // 查找数据
	        $data = M('level')->find($id);

	        $socreArr = array();
	        $socreArr = json_decode("{$data['socre']}");
	        $socre1	= $socreArr->min;
	        $socre2 = $socreArr->max;

	        $val = array_merge($data,array('socre1'=>$socre1,'socre2'=>$socre2));

	        $this->assign('title','修改积分页');
	        $this->assign('data',$val);
	        $this->display();
	    }

	    // 执行修改
	    public function update()
	    {
	    	$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize = 3145728 ;// 设置附件上传大小
			$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
			// 上传单个文件
			$info = $upload->uploadOne($_FILES['img']);

	        // 过滤数据
	        $level = D('level');

	        if($_POST['socre1'] > $_POST['socre2']){

	        	$max = $_POST['socre1'];
	        	$min = $_POST['socre2'];

	        }else if($_POST['socre1'] < $_POST['socre2']){
	        	$max = $_POST['socre2'];
	        	$min = $_POST['socre1'];
	        }

	        $range = ['min'=>$min,'max'=>$max];

	        $range = json_encode($range);

			// 保存当前数据对象
			$_POST['img'] = $info['savepath'].$info['savename'];
	        
	        $data = array('level'=>$_POST['level'],'img'=>$_POST['img'],'socre'=>$range,'id'=>$_GET[id]);

	        if ($level->save($data) > 0) {
                $this->success('恭喜您,编辑成功!',U('level'));
            } else {
                $this->error('编辑失败!');
            }
	    }
	}